from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import reactor
from common.netids import *

import sys
import cPickle as pickle

class PClientProtocol(Protocol, PacketHandler):
    def connectionMade(self):
        print "We art connected!!"
        self.sendData(PID_LOGIN, (LOGIN_REQUEST, "urthen", "rampage"))

    def dataRecieved(self, data):
        self.cacheData(data)
        
class PClientNet(ClientFactory):
    '''This class handles networking with the Twisted library.'''
        
    def startedConnecting(self, connector):
        print "Started connecting."
        
    def buildProtocol(self, addr):
        print "Connected to", addr
        self._protocol = PClientProtocol()
        return self._protocol
    
    def clientConnectionLost(self, connector, reason):
        print "Lost connection, reason:", reason
        sys.exit()
        
    def clientConnectionRefused(self, connector, reason):
        print "Connection Refused, reason:", reason
